package cn.zjh.system.controller;

import cn.zjh.system.query.SystemTypeQuery;
import cn.zjh.system.service.ISystemTypeService;
import cn.zjh.base.utils.AjaxResult;

import cn.zjh.base.utils.PageList;
import cn.zjh.system.domain.SystemType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 部门表象层
* @Author: Mr.Zhang
* @Version: 1.0
* @Date:  2022/6/24 13:10
*/
@RestController
@RequestMapping("/systemType")
public class SystemTypeController {


    @Autowired
    private ISystemTypeService systemTypeService;


    /**
    * @Title: loadById
    * @Description: 查询所有
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 13:18
    * @Parameters:
    * @Return
    */
    @GetMapping()
    public List<SystemType> loadAll() {
        return systemTypeService.loadAll();
    }

    /**
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:15
     * @Parameters:
     * @Return
     */
    @GetMapping("/{id}")
    public SystemType loadById(@PathVariable("id") Long id) {
        return systemTypeService.loadById(id);
    }


    /**
     * @Title: remove
     * @Description: 删除
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:22
     * @Parameters:
     * @Return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            systemTypeService.cascadingDel(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    /**
     * @Title: addOrUpt
     * @Description: 新增/修改
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:26
     * @Parameters:
     * @Return
     */

    @PutMapping()
    public AjaxResult addOrUpt(@RequestBody SystemType systemType){
        try {
            if (systemType.getId() == null){
                systemTypeService.save(systemType);
            } else {
                systemTypeService.update(systemType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    /**
     * @Title: remove
     * @Description: 高级查询+分页查询
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:28
     * @Parameters:
     * @Return
     */
    @PostMapping()
    public PageList<SystemType> queryData(@RequestBody SystemTypeQuery systemTypeQuery){

        PageList<SystemType> data = systemTypeService.queryData(systemTypeQuery);
        return data;
    }


    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/25 22:12
     * @Parameters:
     * @Return
     */
    @PatchMapping()
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            systemTypeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }



}
